package com.spr.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.spr.exception.ScheduleNotFound;
import com.spr.exception.StudentNotFound;
import com.spr.model.Schedule;
import com.spr.model.Student;
import com.spr.model.StudentRegister;
import com.spr.model.User;
import com.spr.service.ApplicationService;
import com.spr.service.ScheduleService;
import com.spr.service.StudentService;

@Controller
@RequestMapping(value = "/student")
public class StudentController {

	@Autowired
	private StudentService studentService;

	@Autowired
	private ScheduleService scheduleService;
	
	@Autowired
	private ApplicationService applicationService;

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newstudentPage(HttpSession session) {
		ModelAndView mav = new ModelAndView("student-new", "studentRegister",
				new StudentRegister());
		return mav;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewstudent(
			@ModelAttribute @Valid StudentRegister student,
			BindingResult result, final RedirectAttributes redirectAttributes, HttpSession session) {
		
		
		User n = (User)session.getAttribute("UserDetails");
		n.getUserID();
		  
		Schedule d = scheduleService.create(student.getSchedule());		
		student.getStudent().setScheduleID(d.getScheduleID());
		student.getStudent().setUserID(n.getUserID());
		student.setStudent(studentService.create(student.getStudent()));
		
		ModelAndView mav = new ModelAndView("student-view", "StudentRegister",
				student);
		mav.addObject("student", student.getStudent());
		mav.addObject("schedule", student.getSchedule());
		return mav;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView studentListPage(HttpSession session) {
		User p = (User)session.getAttribute("UserDetails");
		
		ModelAndView mav = new ModelAndView("student-list");
		List<Student> studentList = studentService.findAll();
		mav.addObject("studentList", studentList);
		mav.addObject("type", p.getType());
		return mav;
	}

	@RequestMapping(value = "/edit/{studentID}", method = RequestMethod.GET)
	public ModelAndView editstudentPage(@PathVariable Integer studentID, HttpSession session) {
		StudentRegister s = new StudentRegister();
		
		Student z = studentService.findById(studentID);
		s.setStudent(z);
		Schedule schedule = scheduleService.findById(z.getScheduleID());
		s.setSchedule(schedule);
		ModelAndView mav = new ModelAndView("student-edit", "studentRegister", s);
		mav.addObject("student",studentService.findById(studentID));
		mav.addObject("schedule",scheduleService.findById(z.getScheduleID()));
		return mav;
	}

	@RequestMapping(value = "/edit/{studentID}", method = RequestMethod.POST)
	public ModelAndView editstudent(@ModelAttribute @Valid StudentRegister student,
			BindingResult result, @PathVariable Integer studentID,
			final RedirectAttributes redirectAttributes, HttpSession session) throws StudentNotFound, ScheduleNotFound {
		//
		// if (result.hasErrors())
		// return new ModelAndView("student-edit");
		
		User u = (User) session.getAttribute("UserDetails");
		
		student.getStudent().setStudentID(studentID);
			student.getSchedule().setScheduleID(studentService.findById(studentID).getScheduleID());
		ModelAndView mav = new ModelAndView("student-view", "studentRegister", student);
		
		
		mav.addObject("student", student.getStudent());
		mav.addObject("schedule", student.getSchedule());
		mav.addObject("type", u.getType());
		scheduleService.update(student.getSchedule());
		studentService.update(student.getStudent());
		
		return mav;
	}

	@RequestMapping(value = "/delete/{studentID}/{scheduleID}", method = RequestMethod.GET)
	public ModelAndView deletestudent(@PathVariable Integer studentID, @PathVariable Integer scheduleID,
			final RedirectAttributes redirectAttributes, HttpSession session) throws StudentNotFound {

		ModelAndView mav = new ModelAndView("redirect:/student/list.html");

		try {

			studentService.delete(studentID);
			scheduleService.delete(scheduleID);
			
		} catch (ScheduleNotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return mav;
	}

	@RequestMapping(value = "/view/{studentID}", method = RequestMethod.GET)
	public ModelAndView viewStudentPage(@PathVariable Integer studentID, HttpSession session) {
		StudentRegister r = new StudentRegister();
		User n = (User) session.getAttribute("UserDetails");
		Student z = studentService.findById(studentID);
		
		Schedule s = scheduleService.findById(z.getScheduleID());
		
		r.setStudent(z);
		r.setSchedule(s);
		ModelAndView mav = new ModelAndView("student-view", "studentRegister", r);
		mav.addObject("student", studentService.findById(studentID));
		mav.addObject("schedule", scheduleService.findById(studentService
				.findById(studentID).getScheduleID()));
		mav.addObject("type", n.getType());
		return mav;
	}
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public ModelAndView viewStudent(HttpSession session) {
		User n = (User) session.getAttribute("UserDetails");
		
		int userID = n.getUserID();
		
		StudentRegister r = new StudentRegister();
		
		Student z = studentService.findByUserID(userID);
		
		Schedule s = scheduleService.findById(z.getScheduleID());
		
		r.setStudent(z);
		r.setSchedule(s);
		ModelAndView mav = new ModelAndView("student-view", "studentRegister", r);
		mav.addObject("student", z);
		mav.addObject("schedule", scheduleService.findById(studentService
				.findById(z.getStudentID()).getScheduleID()));
		mav.addObject("type", n.getType());
		return mav;
	}

	@RequestMapping(value ="/apply/{positionID}")
	public ModelAndView apply(@PathVariable Integer positionID, HttpSession session) {
		
		User n = (User) session.getAttribute("UserDetails");
		
		Student z = studentService.findByUserID(n.getUserID());
		int studentID = z.getStudentID();
		applicationService.apply(studentID, positionID);
		ModelAndView mav = new ModelAndView("student-view");
		mav.addObject("student", studentService.findById(studentID));
		mav.addObject("schedule", scheduleService.findById(studentService.findById(studentID).getScheduleID()));
		mav.addObject("type", n.getType());
		return mav;
	}
	


}
